package livesun.justiceeternal.view.titlebar;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * 类描述：TitleBar的基类
 * 创建人：livesun
 * 创建时间：2017/8/29
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */

public abstract class AbsTitleBar<T extends AbsTitleBarParams> implements ITitleBar{

    private T mParams;
    private View mTitleBar;

    public AbsTitleBar(T params){
        mParams = params;
        //初始化View
        initView();
        //绑定View
        bindView();
    }

    protected  void initView(){
        //获取DecorView的第一个子控件--LinearLayout
        if(mParams.parent==null){
            ViewGroup decorView = (ViewGroup) mParams.activity.getWindow().getDecorView();
            mParams.parent = (ViewGroup) decorView.getChildAt(0);
        }
        if(mParams.parent==null){
            throw new IllegalArgumentException("找不到系统的DecorView");
        }
        //把布局id填充成mTitleBar，并添加进mParams.parent中
        mTitleBar = LayoutInflater.from(mParams.activity).inflate(layoutId(), mParams.parent, false);

        if(mTitleBar==null){
            throw new IllegalArgumentException("mTitleBar为null,请检查原因");
        }
        mParams.parent.addView(mTitleBar,0);
    }

    //========0公共的方法 ======

    /**
     * 4.4 只有小米和魅族支持黑色字体。6.0全支持
     * @param title_root titlbar
     * @param statusColor 状态栏颜色
     * @param isDark 是否黑色字体
     */
    protected void isTranslucentUtils(int title_root,int statusColor,boolean isDark){
        if(statusColor!=0){
           View titleBar=findViewById(title_root);
            if(Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
                if(titleBar != null) {
                    mParams.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    ViewGroup.LayoutParams params = titleBar.getLayoutParams();
                    int statusHeight = TranslucentUtils.getStatusHeight();
                    params.height += statusHeight;
                    titleBar.setLayoutParams(params);
                    titleBar.setPadding(titleBar.getPaddingLeft(),
                            titleBar.getPaddingRight(),
                            titleBar.getPaddingTop() + statusHeight, titleBar.getPaddingBottom());
                    titleBar.setBackgroundColor(statusColor);
                }
            }else if(Build.VERSION.SDK_INT >= 21){
                mParams.activity.getWindow().setStatusBarColor(statusColor);
                titleBar.setBackgroundColor(statusColor);
            }

            if(isDark){
                //小于6.0或者大于4.4
                if(Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 23){
                    TranslucentUtils.FlymeSetStatusBarLightMode(mParams.activity.getWindow(),isDark);
                    TranslucentUtils.MIUISetStatusBarLightMode(mParams.activity.getWindow(),isDark);
                }else if(Build.VERSION.SDK_INT >= 23){
                    //大于6.0设置字体为黑色。。试用状态栏为白底黑字
                    mParams.activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }
            }
        }

    }

    protected <T extends View> T findViewById(int viewId){
        View view = mTitleBar.findViewById(viewId);
        return (T) view;
    }

    //设置文本
    protected void setText(CharSequence text,int viewId){
        TextView textView=findViewById(viewId);
        if(!TextUtils.isEmpty(text)){
            textView.setText(text);
            textView.setVisibility(View.VISIBLE);
        }
    }
    //设置点击事件
    protected void setEvent(int viewId,View.OnClickListener listener){
        if(listener!=null){
            findViewById(viewId).setOnClickListener(listener);
        }
    }
    //设置背景
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    protected void setBackground(int viewId, Bitmap background){
        if(background!=null){
            BitmapDrawable bd=new BitmapDrawable(background);
            findViewById(viewId).setBackground(bd);
        }
    }
    //设置背景
    protected void setBackground(int viewId, int backgroundColor){
        if(backgroundColor!=0){
            findViewById(viewId).setBackgroundColor(backgroundColor);
        }
    }
    //设置左边图片
    protected void setLeftIcon(int viewId, int resId){
        if(resId!=0){
            ImageView imageView = findViewById(viewId);
            imageView.setImageResource(resId);
        }else{
            ImageView imageView = findViewById(viewId);
            imageView.setVisibility(View.GONE);
        }
    }



    protected T getParams(){

        return mParams;
    }

    //内部类builder
    protected abstract static class Builder{
        public Builder(Activity context){}
        public Builder(Activity context,ViewGroup parent){}
        public abstract AbsTitleBar creat();

    }

}
